package xidw_service

import (
	"server/apps/xidw/xidw_mod"
	systemReq "server/apps/xidw/xidw_mod/request"
	"server/global"
	"server/model/common/request"
)


type OperationRecordService struct{}

func (operationRecordService *OperationRecordService) CreateSysOperationRecord(sysOperationRecord xidw_mod.SysOperationRecord) (err error) {
	if len(sysOperationRecord.Body) > 1000 {
		sysOperationRecord.Body = sysOperationRecord.Body[0:999]
	}
	err = global.SysDB.Create(&sysOperationRecord).Error
	return err
}


func (operationRecordService *OperationRecordService) DeleteSysOperationRecordByIds(ids request.IdsReq) (err error) {
	err = global.SysDB.Delete(&[]xidw_mod.SysOperationRecord{}, "id in (?)", ids.Ids).Error
	return err
}


func (operationRecordService *OperationRecordService) DeleteSysOperationRecord(sysOperationRecord xidw_mod.SysOperationRecord) (err error) {
	err = global.SysDB.Delete(&sysOperationRecord).Error
	return err
}


func (operationRecordService *OperationRecordService) GetSysOperationRecord(id uint) (sysOperationRecord xidw_mod.SysOperationRecord, err error) {
	err = global.SysDB.Where("id = ?", id).First(&sysOperationRecord).Error
	return
}


func (operationRecordService *OperationRecordService) GetSysOperationRecordInfoList(info systemReq.SysOperationRecordSearch) (list interface{}, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
	db := global.SysDB.Model(&xidw_mod.SysOperationRecord{})
	var sysOperationRecords []xidw_mod.SysOperationRecord
	if info.Method != "" {
		db = db.Where("method = ?", info.Method)
	}
	if info.Path != "" {
		db = db.Where("path LIKE ?", "%"+info.Path+"%")
	}
	if info.Status != 0 {
		db = db.Where("status = ?", info.Status)
	}
	err = db.Count(&total).Error
	if err != nil {
		return
	}
	err = db.Order("id desc").Limit(limit).Offset(offset).Preload("User").Find(&sysOperationRecords).Error
	return sysOperationRecords, total, err
}
